<template>
  <div class="app-container">
    <h2 class="color2">经销商名称：{{tableDate1[0].distributorName}}</h2>
    <!--工具栏-->
    <div class="head-container">
      <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery" />
      <el-select v-model="query.type" clearable placeholder="搜索类型" class="filter-item" style="width: 130px">
        <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
    </div>
    <!--表单组件-->
    <eForm ref="form" :is-add="isAdd" />
    <!--表格渲染-->
    <el-table v-loading="loading" :data="tableDate1" :span-method="objectSpanMethod" size="small" style="width: 100%;">
<!--      <el-table-column prop="productId" label="序号" />-->
      <el-table-column prop="productName" label="产品名称" />
      <el-table-column ref="table" prop="productImage" label="产品图片">
        <template slot-scope="scope">
          <a :href="scope.row.productImage" style="color: #42b983" target="_blank"><img :src="scope.row.productImage" alt="点击打开" class="el-avatar"></a>
        </template>
      </el-table-column>
      <el-table-column prop="productSkuName" label="SKU名称" />
      <el-table-column ref="table" prop="productSkuImage" label="SKU图片">
        <template slot-scope="scope">
          <a :href="scope.row.productSkuImage" style="color: #42b983" target="_blank"><img :src="scope.row.productSkuImage" alt="点击打开" class="el-avatar"></a>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="零售价" />
      <el-table-column prop="discount" label="折扣" />
      <el-table-column prop="tradePrice" label="供货价" />


      <el-table-column v-if="checkPermission(['admin','YXDISTRIBUTOR_EDIT'])" label="操作" width="220px" align="center">
        <template slot-scope="scope">
          <el-button
            v-permission="['admin','YXDISTRIBUTOR_EDIT']"
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="edit(scope.row)"
          >编辑</el-button>
          <el-popover
            :ref="scope.row.id"
            v-permission="['admin','YXDISTRIBUTOR_EDIT']"
            placement="top"
            width="180"
          >
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini" />
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/crudspecial'
import { del } from '@/api/yxDistributorPrice'
import eForm from './priceform'
import { formatTime } from '@/utils/index'
import { getAll } from "@/api/yxDistributorPrice"
import qs from 'qs'

export default {
  components: { eForm },
  mixins: [initData],
  data() {
    return {
      delLoading: false,
      tableDate1:null,
      spanArr: []
    };
  },

  /*mounted() {
    let resp = getAll(this.$route.params.id)
    debugger
    this.tableDate1 = resp.data
    this.getSpanArr(this.tableDate1);
  },*/



  created() {
    this.$nextTick(() => {
      this.init()
      /*this.params = { page: this.page, size: this.size, sort: 'id,desc' },
      getAll(this.$route.params.id,this.params).then(resp=>{
        debugger
        this.tableDate1 = resp.content,
        this.spanArr = [],
        this.getSpanArr(this.tableDate1)
      })*/
    })
  },
  methods: {
    formatTime,
    checkPermission,
    beforeInit(){
      debugger
      this.url = 'api/getDistributorPricesByDisId/'+this.$route.params.id
      const sort = 'id,desc'
      this.params = { page: this.page, size: this.size, sort: sort }
      return true
    },

    special(){
      debugger
      this.tableDate1 = this.data
      this.getSpanArr(this.tableDate1)
    },

    getSpanArr(data) {
      debugger
      // data就是我们从后台拿到的数据
      for (let i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr.push(1);
          this.pos = 0;
        } else {
          // 判断当前元素与上一个元素是否相同
          if (data[i].productId === data[i - 1].productId) {
            this.spanArr[this.pos] += 1;
            this.spanArr.push(0);
          } else {
            this.spanArr.push(1);
            this.pos = i;
          }
        }
        console.log(this.spanArr);
      }
    },

    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 1) {
        const _row = this.spanArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        //console.log(`rowspan:${_row} colspan:${_col}`);
        return {
          // [0,0] 表示这一行不显示， [2,1]表示行的合并数
          rowspan: _row,
          colspan: _col
        };
      }
    },














    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.dleChangePage()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    },
    subPublish(id) {
      this.delLoading = true
      publish(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.dleChangePage()
        this.init()
        this.$notify({
          title: '发布成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    },
    add() {
      this.isAdd = true
      this.$refs.form.dialog = true
    },
    edit(data) {
      debugger
      this.isAdd = false
      const _this = this.$refs.form
      _this.form = {
        id: data.id,
        productName: data.productName,
        productSkuName: data.productSkuName,
        price: data.price,
        discount: data.discount,
        tradePrice: data.tradePrice,
      }
      _this.dialog = true
    }
  }
}
</script>

<style scoped>

</style>
